package cn.iocoder.yudao.module.water.service.day1;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.water.controller.admin.day1.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.day1.ReportDay1DO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.water.convert.day1.ReportDay1Convert;
import cn.iocoder.yudao.module.water.dal.mysql.day1.ReportDay1Mapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.water.enums.ErrorCodeConstants.*;

/**
 * 日报表1 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class ReportDay1ServiceImpl implements ReportDay1Service {

    @Resource
    private ReportDay1Mapper reportDay1Mapper;

    @Override
    @DS("report")
    public Long createReportDay1(ReportDay1CreateReqVO createReqVO) {
        // 插入
        ReportDay1DO reportDay1 = ReportDay1Convert.INSTANCE.convert(createReqVO);
        reportDay1Mapper.insert(reportDay1);
        // 返回
        return reportDay1.getId();
    }

    @Override
    @DS("report")
    public void updateReportDay1(ReportDay1UpdateReqVO updateReqVO) {
        // 校验存在
        this.validateReportDay1Exists(updateReqVO.getId());
        // 更新
        ReportDay1DO updateObj = ReportDay1Convert.INSTANCE.convert(updateReqVO);
        reportDay1Mapper.updateById(updateObj);
    }

    @Override
    @DS("report")
    public void deleteReportDay1(Long id) {
        // 校验存在
        this.validateReportDay1Exists(id);
        // 删除
        reportDay1Mapper.deleteById(id);
    }

    private void validateReportDay1Exists(Long id) {
        if (reportDay1Mapper.selectById(id) == null) {
            throw exception(REPORT_DAY1_NOT_EXISTS);
        }
    }

    @Override
    @DS("report")
    public ReportDay1DO getReportDay1(Long id) {
        return reportDay1Mapper.selectById(id);
    }

    @Override
    @DS("report")
    public List<ReportDay1DO> getReportDay1List(Collection<Long> ids) {
        return reportDay1Mapper.selectBatchIds(ids);
    }

    @Override
    @DS("report")
    public PageResult<ReportDay1DO> getReportDay1Page(ReportDay1PageReqVO pageReqVO) {
        return reportDay1Mapper.selectPage(pageReqVO);
    }

    @Override
    @DS("report")
    public List<ReportDay1DO> getReportDay1List(ReportDay1ExportReqVO exportReqVO) {
        return reportDay1Mapper.selectList(exportReqVO);
    }

    @Override
    @DS("report")
    public List<ReportDay1DO> getReportOneDay1List(String date) {
        DateTime dateTime = DateUtil.parseDateTime(date);
        int year = DateUtil.year(dateTime);
        String db1 = "DR1" + year;
        String db2 = "DR2" + year;
        String start = String.valueOf(DateUtil.beginOfDay(dateTime));
        String end = String.valueOf(DateUtil.endOfDay(dateTime));
        return reportDay1Mapper.selectOneDay1List(db1, db2, start, end);
    }

    @Override
    @DS("report")
    public List<ReportDay1DO> getReportNDay1List(String startTime, String endTime) {
        DateTime st = DateUtil.parse(startTime, "yyyy-MM-dd");
        DateTime et = DateUtil.parse(endTime,"yyyy-MM-dd");
        int year = DateUtil.year(st);
        String db1 = "DR1" + year;
        String db2 = "DR2" + year;
        String start = String.valueOf(DateUtil.beginOfDay(st));
        String end = String.valueOf(DateUtil.endOfDay(et));
        return reportDay1Mapper.selectOneDay1List(db1, db2, start, end);
    }

}
